﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace HTCCoverImporter
{
    public class CoverImporter
    {
        public string Source { get; set; }
        public string Destination { get; set; }

        private int PubCount { get; set; }
        private string _currPub;

        public List<string> ImportCovers(out string output)
        {
            List<string> lstCovers = new List<string>();
            output = "";
            int count = 0;

            string[] sourceDirs = Directory.GetDirectories(Source);

            
            for (int i = 0; i <= sourceDirs.Length - 1; i++)
            //for (int i = 0; i < 2; i++)
            {
                string[] publisher = Directory.GetDirectories(sourceDirs[i]);
                //count += publisher.Length;                

                for (int j = 0; j <= publisher.Length - 1; j++)
                {
                    string[] comics = Directory.GetDirectories(publisher[j]);
                    //count += comics.Length;                

                    for (int k = 0; k <= comics.Length - 1; k++)
                    {
                        string dirComic = comics[k];

                        string[] subComics = Directory.GetDirectories(dirComic);
                        
                        try
                        {
                            if (subComics.Length > 1)
                            {
                                for (int l = 0; l <= subComics.Length - 1; l++)
                                {
                                    string subComic = subComics[l];
                                    string dirToCreate = subComic.Replace(Path.GetDirectoryName(subComic), "").Replace("\\", "");
                                    string initial = dirToCreate[0].ToString();
                                    //Directory.CreateDirectory(Destination + "\\" + initial + "\\" + dirToCreate);

                                    string[] covers = Directory.GetFiles(subComic);
                                    for (int iCovers = 0; iCovers <= covers.Length - 1; iCovers++)
                                    {
                                        //File.Copy(covers[iCovers], Destination + "\\" + initial + "\\" + subComic.Replace(Path.GetDirectoryName(subComic), "") + "\\" + Path.GetFileName(covers[iCovers]));
                                        lstCovers.Add(covers[iCovers]);
                                    }
                                }
                            }
                            else
                            {
                                string dirToCreate = dirComic.Replace(Path.GetDirectoryName(dirComic), "").Replace("\\","");
                                string initial = dirToCreate[0].ToString();
                                //Directory.CreateDirectory(Destination + "\\" + initial + "\\" + dirToCreate);

                                string []covers = Directory.GetFiles(dirComic);
                                for (int iCovers = 0; iCovers <= covers.Length - 1; iCovers++)
                                {
                                    //File.Copy(covers[iCovers], Destination + "\\" + initial + "\\" + dirComic.Replace(Path.GetDirectoryName(dirComic), "") + "\\" + Path.GetFileName(covers[iCovers]));
                                    lstCovers.Add(covers[iCovers]);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            output += ex.ToString() + "\n\n\n";
                        }
                    }
                }
            }

            //output += count.ToString();
            return lstCovers;
        }

        private void PublisherExport()
        {
            string[] dirs = Directory.GetDirectories(_currPub);

            for (int i = 0; i <= dirs.Length - 1; i++)
            {
                PubCount += 1;
            }
        }
    }
}
